<template>
  <div>
    <router-view />
    <div class="slot"></div>
    <div class="bottombar">
      <div
        v-for="(item, index) in barList"
        @click="goOtherPage(item.path)"
        :key="index"
      >
        <img :src="item.img" alt="" v-if="!item.active" />
        <img :src="item.imgActive" alt="" v-else />
        <div :style="{ color: item.active ? '#25fa33' : '#333' }">
          {{ item.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import homeImg from "@/assets/home.png";
import homeActiveImg from "@/assets/home_active.png";
import shopCartImg from "@/assets/gouwu.png";
import shopCartActiveImg from "@/assets/gouwu_active.png";
export default {
  data() {
    return {
      barList: [
        {
          name: "首页",
          path: "/home",
          img: homeImg,
          imgActive: homeActiveImg,
          active: false,
        },
        {
          name: "购物车",
          path: "/shopCart",
          img: shopCartImg,
          imgActive: shopCartActiveImg,
          active: false,
        },
      ],
    };
  },
  mounted() {
    // // 获取元素的样式属性值
    // const element = document.querySelector(".bottombar");
    // const computedStyles = getComputedStyle(element);
    // const primaryColorValue = computedStyles
    //   .getPropertyValue("--barLength")
    //   .trim();
    // console.log(primaryColorValue);

    this.barList.forEach((item) => {
      if (this.$route.path === item.path) {
        item.active = true;
      }
    });
  },
  methods: {
    goOtherPage(path) {
      this.$router.push(path);
      this.barList.forEach((item) => {
        if (item.path === path) {
          item.active = true;
        } else {
          item.active = false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.slot{
  height: 13.333333vw;
}
.bottombar {
  // 这里的变量 barList.length 是一个动态变量，需要用 v-bind 绑定
  --barLength: v-bind("barList.length");
  position: fixed;
  bottom: 0;
  width: 100vw;
  height: 13.333vw;
  z-index: 100;
  display: flex;
  background-color: #fff;
  justify-content: space-around;
  align-items: center;
  font-size: 3.733vw;
  & > div {
    text-align: center;
    // var用于接收动态变量的值
    width: calc(100vw / var(--barLength));
    img {
      width: 6.667vw;
      height: 6.667vw;
      object-fit: cover;
    }
  }
}
</style>